﻿using System;
using MaLu.Common;
using System.Data;
using MaLu.BusiLogic;
using Wicresoft.Framework.Organization;
using MaLu.Data;

namespace MaluJg.web.FarmerShare
{
    public partial class FSList : FSPage
    {

        public string SearchClauseItem
        {
            get
            {
                string where = "1=1";

                if (IsFSAdmin)
                {
                    if (tbVillage.Text.Trim() != string.Empty)
                    {
                        where += " and Village like '%" + tbVillage.Text.Trim().Replace("'", "''") + "%'";

                    }
                }               
                else
                {
                    where += " and Village ='" + CurrentUser.GetMainDepartment().DisplayName + "'";
                }

                if (tbName.Text.Trim() != string.Empty)
                {
                    where += " and FamilyName like '%" + tbName.Text.Trim().Replace("'", "''") + "%'";

                }
               
                if (tbYear.Text.Trim() != string.Empty)
                {
                    where += " and Year <= '" + tbYear.Text.Trim() + "'";

                }
                return where;
            }
             
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "农民股金列表";


            tbYear.Attributes.Add("readonly", "readonly");

            if (!IsPostBack)
            {
               
                
            }

        }

        protected override void Pager_PageChanged(object sender, EventArgs e)
        {
            BindData();
            tbIDs.Text = string.Empty;
            base.Pager_PageChanged(sender, e);
        }

        protected override void Order_OrderChanged(object sender, EventArgs e)
        {
            BindData();
            base.Order_OrderChanged(sender, e);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {

            Pager.PageIndex = 0;

            if (tbIDs.Text.Trim() != string.Empty)
            {
                string sql = string.Format("delete from [FaShare] where id in ('{0}');", tbIDs.Text.Trim().Substring(1).Replace(",", "','"));

                SqlHelper.Instance.ExecuteNonQuery(Provider, sql);
                tbIDs.Text = string.Empty;
            }

            BindData();
        }

        private void BindData()
        {
            int recordCount = 0;
            DataTable DTMyProposal = BLCommon.GetPagingList(Provider, "ID,[realget],[FamilyName],[Village],Year", "FaShare", SearchClauseItem, "Year  desc,village,place,familyname", Pager.PageIndex + 1, Pager.PageSize, false, 0, ref recordCount);
            Pager.RecordCount = recordCount;
            rptBusi.DataSource = DTMyProposal;
            rptBusi.DataBind();
        }

        protected void btnAdvancedSearch_Click(object sender, EventArgs e)
        {
            
            BindData();
        }
    }
}
